package com.thinksns.sociax.thinksnsbase.utils;

import android.content.Context;
import android.text.Editable;
import android.text.Html;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

import java.io.UnsupportedEncodingException;

public class WordCount implements TextWatcher {
    public static final int MAX_COUNT = 140;
    private static final String TAG = "WordCount";

    private TextView overWordCount;        //输入文字个数提示器
    private EditText editText;
    private InputFilter mInputFilter;
    private CharSequence temp;
    private int selectionStart;
    private int selectionEnd;
    private String tran;
    private Context context;
    /**
     * 输入是否符合限制
     ***/
    private boolean isValid = true;
    private int mWordCount = MAX_COUNT; // 可输入最大字数，m默认 140
    private boolean canInputOverCount = true; // 超出最大输入限制，是否可继续输入，默认 true，能继续输入
    private TextChangeListener mTextChangeListener;

    public WordCount(EditText text, TextView v) {
        this.overWordCount = v;
        this.editText = text;
    }

    public WordCount(EditText text, TextView v, String tran) {
        this.overWordCount = v;
        this.editText = text;
        this.tran = tran;
        limit(tran);
    }

    public WordCount(EditText text, int maxCount, boolean canInputOverCount) {
        this.editText = text;
        this.canInputOverCount = canInputOverCount;
        this.mWordCount = maxCount;
    }

    public void setTextChangeListener(TextChangeListener textChangeListener) {
        mTextChangeListener = textChangeListener;
    }

    public int getMaxCount() {
        return mWordCount;
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
                                  int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        temp = s;
    }

    @Override
    public void afterTextChanged(Editable s) {
        if (mTextChangeListener != null) {
            mTextChangeListener.textChange(s);
        }
        //	修改输入内容计算方式，按照实际站位来计算长度，与PC端统一  by Catherine 2016年12月2日
        int len = 0;
        try {
            len = s.toString().getBytes("GBK").length;
            len = (len % 2 == 0) ? len / 2 : len / 2 + 1;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            if (editText != null && mInputFilter == null) {
                mInputFilter = new InputFilter.LengthFilter(mWordCount);
                editText.setFilters(new InputFilter[]{mInputFilter});
            }
        }
        int number = mWordCount - len;
        selectionStart = editText.getSelectionStart();
        selectionEnd = editText.getSelectionEnd();
        /*this part for publish weibo or some need show */
        if (overWordCount != null) {
            if (number <= 10) {
                String over = "<font color='red'>" + number + "</font>";
                this.overWordCount.setText(Html.fromHtml(over));
                if (number < 0) {
                    isValid = false;
                    return;
                }
            } else {
                this.overWordCount.setText("" + number);
            }
        }
        /* if over count*/
        if (len != 0 && len > mWordCount) {
            if (!canInputOverCount && editText != null) {
                /*over count android cant inputting, reset text*/
                s.delete(selectionStart - 1, selectionEnd);
                int tempSelection = (selectionStart == 0) ? s.length() : selectionStart;
                if (tempSelection > s.length()) {
                    tempSelection = s.length();
                }
                editText.setText(s);
                editText.setSelection(tempSelection);

            }
            /*over count call back*/
            if (mTextChangeListener != null) {
                mTextChangeListener.textOverCount(MAX_COUNT);
            }
        }
        isValid = true;

    }

    public void limit(String tran) {
        int number = mWordCount - tran.length();

        if (overWordCount != null) {
            if (number <= 10) {
                String over = "<font color='red'>" + number + "</font>";
                this.overWordCount.setText(Html.fromHtml(over));
            } else {
                this.overWordCount.setText("" + number);
            }
        }

        editText.setText(tran);
        editText.setSelection(tran.length());        // 设置光标
    }

    /**
     * 输入是否有效
     *
     * @return
     */
    public boolean inputValid() {
        return this.isValid;
    }

    public interface TextChangeListener {
        void textOverCount(int maxCount);

        void textChange(Editable editable);
    }
}
